package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.friendship.TIMFriend;
import com.tencent.qcloud.tim.uikit.component.AudioPlayer;
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.ChatLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.input.InputLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.MessageLayout;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.helper.ChatLayoutHelper;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.chat.FriendProfileActivity;
import com.www.applyword.utils.SPUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/11/16 0016 11:12
 * version: 1.0
 */
public class ChatFragment2 extends BaseFragment {
    ChatLayout mChatLayout;
    private ChatInfo mChatInfo;
    private TitleBarLayout mTitleBar;

    @Override
    protected int getLayoutResource() {
        return R.layout.chat_fragment2;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        mChatInfo = (ChatInfo) bundle.getSerializable(Constants.CHAT_INFO);
        mChatLayout =  rootView.findViewById(R.id.chat_layout);

        initView();
    }

    private void initView() {
        // TODO 通过api设置ChatLayout各种属性的样例

        // 单聊面板的默认 UI 和交互初始化
        mChatLayout.initDefault();
        /*
         * 需要聊天的基本信息
         */
        mChatLayout.setChatInfo(mChatInfo);

        // TODO 通过api设置ChatLayout各种属性的样例
       ChatLayoutHelper.customizeChatLayout(getActivity(), mChatLayout);

        //获取单聊面板的标题栏
        mTitleBar = mChatLayout.getTitleBar();
      //  mTitleBar.setVisibility(View.GONE);

        // 从 ChatLayout 里获取 InputLayout
        InputLayout inputLayout = mChatLayout.getInputLayout();

        Button send_btn = inputLayout.findViewById(R.id.send_btn);

        EditText chat_message_input = inputLayout.findViewById(R.id.chat_message_input);

        ImageView voice_input_switch = inputLayout.findViewById(R.id.voice_input_switch);

        // 隐藏发送文件
        inputLayout.disableSendFileAction(true);

        // 隐藏摄像并发送
        inputLayout.disableVideoRecordAction(true);
        if (mChatInfo.getType() == TIMConversationType.C2C){
            TIMFriendshipManager.getInstance().getFriendList(new TIMValueCallBack<List<TIMFriend>>() {
                @Override
                public void onError(int code, String desc) {
                    // TUIKitLog.e(TAG, "getFriendList err code = " + code);
                }

                @Override
                public void onSuccess(List<TIMFriend> timFriends) {
                    //  TUIKitLog.i(TAG, "getFriendList success result = " + timFriends.size());
                    if (timFriends.size() == 0) {
                        voice_input_switch.setVisibility(View.GONE);
                        send_btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Map<String, Object> params = new HashMap<>();
                                params.put("SendUserID", SPUtil.getString(Constants.USERID));
                                params.put("ReceiveUserID",SPUtil.getString(mChatInfo.getId() + Constants.USERID));
                                params.put("Con",chat_message_input.getText().toString().trim());
                                HttpClient.get(getActivity(), Constants.SENDMSG, params,new CallBack<LoginBean>() {
                                    @Override
                                    public void onSuccess(LoginBean result) {
                                        chat_message_input.setText("");

                                    }
                                });
                            }
                        });
                        return;
                    }

                    for (TIMFriend timFriend : timFriends) {
                        if (!mChatInfo.getId().equals(timFriend.getIdentifier())){
                            voice_input_switch.setVisibility(View.GONE);
                            send_btn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Map<String, Object> params = new HashMap<>();
                                    params.put("SendUserID", SPUtil.getString(Constants.USERID));
                                    params.put("ReceiveUserID",SPUtil.getString(mChatInfo.getId() + Constants.USERID));
                                    params.put("Con",chat_message_input.getText().toString().trim());
                                    HttpClient.get(getActivity(), Constants.SENDMSG, params,new CallBack<LoginBean>() {
                                        @Override
                                        public void onSuccess(LoginBean result) {
                                            chat_message_input.setText("");

                                        }
                                    });
                                }
                            });
                        }
                    }
                }
            });
        }


        //单聊面板标记栏返回按钮点击事件，这里需要开发者自行控制
        mTitleBar.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        if (mChatInfo.getType() == TIMConversationType.C2C) {
            mTitleBar.setOnRightClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), FriendProfileActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(TUIKitConstants.ProfileType.CONTENT, mChatInfo);
                    getActivity().startActivity(intent);
                }
            });
        }
        if (null != mChatLayout.getMessageLayout()) {

            mChatLayout.getMessageLayout().setOnItemClickListener(new MessageLayout.OnItemClickListener() {
                @Override
                public void onMessageLongClick(View view, int position, MessageInfo messageInfo) {
                  //  mChatLayout.getMessageLayout().showItemPopMenu(position - 1, messageInfo, view);
                }

                @Override
                public void onUserIconClick(View view, int position, MessageInfo messageInfo) {
                    if (null == messageInfo) {
                        return;
                    }
                    ChatInfo info = new ChatInfo();
                    info.setId(messageInfo.getFromUser());
                    Intent intent = new Intent(getActivity(), FriendProfileActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(TUIKitConstants.ProfileType.CONTENT, info);
                    getActivity().startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        AudioPlayer.getInstance().stopPlay();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mChatLayout.exitChat();
    }
}
